package com.geline.demo.config;

import cn.hutool.core.util.ReflectUtil;
import com.alibaba.excel.annotation.ExcelProperty;
import com.geline.easyexcel.domain.ExcelError;
import com.geline.easyexcel.interfaces.ExcelValidFunction;
import com.geline.easyexcel.utils.ExcelValidUtil;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.List;

/**
 * 自定义检验Excel数据
 *
 * @author: mengxin
 * @date: 2024/1/31 16:17
 */
@Component
public class MyExcelValidFunction extends ExcelValidFunction {

    @Override
    public List<ExcelError> apply(Integer rowIndex, Object data) {
        //检验属性@ExcelValid
        List<ExcelError> list = ExcelValidUtil.valid(rowIndex, data);

        Field field = ReflectUtil.getField(data.getClass(), "pathName");
        if(field!=null){
            ExcelProperty property = field.getAnnotation(ExcelProperty.class);
            if(property!=null){
                int colIndex = property.index();
                //检验用户是否拥有此行政区划权限
                //...
                list.add(new ExcelError(rowIndex, colIndex, "您无权限导入当前地市数据！"));
            }
        }
        return list;
    }
}
